﻿using System;
using System.Collections.Concurrent;
using System.IO;
using log4net.Config;

namespace Loki.Utils
{
    /// <summary>
    /// Loki default logger service.
    /// </summary>
    public class LokiLogService : ILogService
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="LokiLogService"/> class.
        /// </summary>
        public LokiLogService()
        {
            _Loggers = new ConcurrentDictionary<string, ILog>();
            XmlConfigurator.Configure();
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="LokiLogService"/> class.
        /// </summary>
        /// <param name="P_ConfigFilePath">The configuration file path.</param>
        public LokiLogService(string P_ConfigFilePath)
        {
            _Loggers = new ConcurrentDictionary<string, ILog>();
            XmlConfigurator.Configure(new FileInfo(P_ConfigFilePath));
        }

        public void Configure(Stream P_Stream)
        {
            XmlConfigurator.Configure(P_Stream);
        }

        private ConcurrentDictionary<string, ILog> _Loggers;

        /// <summary>
        /// Gets the specified logger.
        /// </summary>
        /// <param name="P_LoggerName">The logger name.</param>
        /// <returns>The specified logger.</returns>
        public ILog GetLogger(string P_LoggerName)
        {
            if (!_Loggers.ContainsKey(P_LoggerName))
            {
                _Loggers[P_LoggerName] = new LoggerProxy(P_LoggerName);
            }

            return _Loggers[P_LoggerName];
        }

        private const string NAME = "Loki default log service";

        private const string _ID = "BCE3C9A8-9808-46bf-B419-D0A4EECA9E2E";

        /// <summary>
        /// Gets the service name.
        /// </summary>
        /// <value>The service name.</value>
        public string Name
        {
            get { return NAME; }
        }

        /// <summary>
        /// Gets the service identifier.
        /// </summary>
        /// <value>The service identifier.</value>
        public Guid ID
        {
            get { return new Guid(_ID); }
        }

        /// <summary>
        /// Releases all resources used by an instance of the <see cref="LokiLogService" /> class.
        /// </summary>
        /// <remarks>
        /// This method calls the virtual <see cref="Dispose(bool)" /> method, passing in <strong>true</strong>, and then suppresses 
        /// finalization of the instance.
        /// </remarks>
        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        ~LokiLogService()
        {
            Dispose(false);
        }

        /// <summary>
        /// Releases the unmanaged resources used by an instance of the <see cref="LokiLogService" /> class and optionally releases the managed resources.
        /// </summary>
        /// <param name="P_Disposing">Set <strong>true</strong> to release both managed and unmanaged resources; <strong>false</strong> to release only unmanaged resources.</param>
        protected virtual void Dispose(bool P_Disposing)
        {
        }
    }
}
